package com.wsoft.hr.pm.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.HrPmRedisKeyConstant;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.hr.pm.entity.HrPmTargetGroupEntity;
import com.wsoft.hr.pm.mapper.HrPmTargetGroupMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2025年02月17日 15:06:35
 */
@Slf4j
@Service
public class HrPmTargetGroupManager extends ServiceImpl<HrPmTargetGroupMapper, HrPmTargetGroupEntity> {


    @Resource
    private RedisService redisService;

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    public boolean save(HrPmTargetGroupEntity entity){
        return super.save(entity);
    }

    /**
     * 根据组织和名称查询
     * @param name
     * @return
     */
    public HrPmTargetGroupEntity getOrgAndName(String name){
        QueryWrapper<HrPmTargetGroupEntity> query = new QueryWrapper<>();
        query.lambda().eq(HrPmTargetGroupEntity::getName,name);
       return getOne(query);
    }



    /**
     * 查询全部
     * @param param
     * @return
     */
    public List<HrPmTargetGroupEntity> listAll(){
        QueryWrapper<HrPmTargetGroupEntity> query = new QueryWrapper<>();
        query.lambda().orderByAsc(HrPmTargetGroupEntity::getSort);
        return list(query);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetRedisCache(type = HrPmRedisKeyConstant.GROUP_INFO,
            expire_sec = HrPmRedisKeyConstant.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public HrPmTargetGroupEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 更新
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(HrPmTargetGroupEntity entity){
        boolean b = super.updateById(entity);
        if(b){
            redisService.del( HrPmRedisKeyConstant.GROUP_INFO + entity.getId());
        }
        return b;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        boolean b = super.removeById(id);
        if(b){
            redisService.del( HrPmRedisKeyConstant.GROUP_INFO + id);
        }
        return b;
    }
}
